# --*-- conding: utf-8 --*--
import requests
from lxml import etree

URL = "http://www.maomijiaoyi.com/index.php?/chanpinliebiao_c_2.html"
URL2 = "http://www.maomijiaoyi.com"

getHtml = requests.get(URL)
html = etree.HTML(getHtml.text.encode('utf-8'))

# 单页数据
detailList = html.xpath("//div[@class='breeds_floor']/div/div/a/@href")  # type list
# print(detailList)

# 翻页判断
endpage = html.xpath("//div[@class='maigou_maigou_page']/a[last()]")
nextpage = html.xpath("//div[@class='maigou_maigou_page']/a[last()-1]")
endpage_url = html.xpath("//div[@class='maigou_maigou_page']/a[last()]/@href")  # type list
nextpage_url = html.xpath("//div[@class='maigou_maigou_page']/a[last()-1]/@href")  # type list
# print(endpage[0].text.replace(' ', ''), endpage_url)  # 末页
#
# print(nextpage[0].text.replace(' ', ''), endpage_url)  # 下一页
if '末页' in endpage[0].text.replace(' ', ''):
    pass


# 详情页数据
def length(*data):
    for d in data:
        print(len(d))


def clean(*data):
    for d in data:
        data = d[0].replace(' ', '')
        data = data.replace('\n', '')
        data = data.replace('\r','')
        return data


def detail(detailList):
    for x in detailList:
        # print(x)
        detailhtml = requests.get(URL2 + x)
        detailhtml = etree.HTML(detailhtml.text.encode('utf-8'))

        # 地理位置
        province = detailhtml.xpath("//div[@class='bread_crumb']/a[last()-2]/span/text()")  # 省
        city = detailhtml.xpath("//div[@class='bread_crumb']/a[last()-1]/span/text()")  # 市

        # 基本信息
        name = detailhtml.xpath("//div[@class='detail_text']/div[1]/text()")  # 猫咪名字
        vistorcount = detailhtml.xpath("//div[@class='info1']/div[1]/span[@class='red'][1]/text()")  # 浏览次数
        pure = detailhtml.xpath("//div[@class='xinxi_neirong']/div[1]/div[2]/div[1]/span/text()")  # 是否纯种
        type = detailhtml.xpath("//div[@class='xinxi_neirong']/div[1]/div[2]/div[2]/span/text()")  # 猫咪品种
        haved = detailhtml.xpath("//div[@class='xinxi_neirong']/div[1]/div[2]/div[3]/span/text()")  # 待售数量
        sex = detailhtml.xpath("//div[@class='xinxi_neirong']/div[1]/div[2]/div[4]/span/text()")  # 猫咪性别
        detail_index = detailhtml.xpath("//div[@class='bread_crumb']/a[last()]/@href")  # 详情页

        # 商家卖家信息
        shopname = detailhtml.xpath("//div[@class='dinming']/text()")  # 店名
        price = detailhtml.xpath("//span[@class='red size_24']/text()")  # 价格 (元)
        person = detailhtml.xpath("//div[@class='user_info']/div[1]/div[2]/text()")  # 联系人
        phone_number = detailhtml.xpath("//div[@class='user_info']//span[2]/text()")  # 电话
        postage = detailhtml.xpath("//div[@class='user_info']/div[3]/div[2]/text()")  # 包邮

        # 健康防御
        vaccine = detailhtml.xpath("//div[@class='xinxi_neirong']/div[2]/div[2]/div[1]/span/text()")  # 疫苗情况
        debug = detailhtml.xpath("//div[@class='xinxi_neirong']/div[2]/div[2]/div[2]/span/text()")  # 驱虫情况
        age = detailhtml.xpath("//div[@class='xinxi_neirong']/div[2]/div[2]/div[3]/span/text()")  # 猫咪年龄
        video = detailhtml.xpath("//div[@class='xinxi_neirong']/div[2]/div[2]/div[4]/span/text()")  # 能否视频

        # print(province, city, name, vistorcount, pure, type, haved, sex, detail_index, shopname, price, person, phone_number, postage, vaccine, debug, age, video)
        # print(clean(province)+','+ clean(city)+','+ clean(name)+','+ clean(vistorcount)+','+ clean(pure)+','+ clean(type)+','+ clean(haved)+','+ clean(sex)+','+ clean(detail_index)+','+ clean(shopname)+','+ clean(price)+','+ clean(person)+','+ clean(phone_number)+','+ clean(postage)+','+ clean(vaccine)+','+ clean(debug)+','+ clean(age)+','+ clean(video)+'\n')
        with open("petdata.txt",'a',encoding='utf-8') as f:
            f.write(clean(province)+','+ clean(city)+','+ clean(name)+','+ clean(vistorcount)+','+ clean(pure)+','+ clean(type)+','+ clean(haved)+','+ clean(sex)+','+ clean(detail_index)+','+ clean(shopname)+','+ clean(price)+','+ clean(person)+','+ clean(phone_number)+','+ clean(postage)+','+ clean(vaccine)+','+ clean(debug)+','+ clean(age)+','+ clean(video)+'\n')

detail()
# print(detailList)
